﻿package com.hh.bss.util;

import java.io.UnsupportedEncodingException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.misc.BASE64Encoder;

/**
 * <p>Title: DES加密算法辅助器</p>
 * <p>Description: </p>
 * @version 1.0
 */
public class DESHelper {
	public static boolean debug = true;
	private static final Log logger = LogFactory.getLog(DESHelper.class);
	private static final String ALGORITHM = "DES";
	public static byte[] desEncrypt(byte[] in, String key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(in);
    }
    public static byte[] desDecode(final byte[] in, String key)  throws Exception{
    	if(in == null || key == null){ return null; }
    	DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		try{
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			byte[] bytes = cipher.doFinal(in);
			return bytes;
		}catch(java.security.NoSuchAlgorithmException nsae){	
			nsae.printStackTrace();
			return null;
		}catch(javax.crypto.NoSuchPaddingException nspe){
			nspe.printStackTrace();
			return null;
		}catch(java.lang.Exception e){	
			e.printStackTrace();
			return null;
		}
    }
    public static String byte2hex(byte[] b) // 二行制转字符串
	{
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}

	public static byte[] hex2byte(String hex) {
		byte[] bts = new byte[hex.length() / 2];
		for (int i = 0; i < bts.length; i++) {
			bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2),
					16);
		}
		return bts;
	}
	public static String  base64Encode(byte[] bs){
    	if (bs == null) return null; 
    	return (new sun.misc.BASE64Encoder()).encode( bs); 
    }
    public static byte[] base64Decode(String s){
    	if (s == null) return null; 
    	sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); 
    	try { 
    	   return decoder.decodeBuffer(s);     	
    	} catch (Exception e) { 
    	   return null; 
    	} 
    }
	

	

}